#include "dbuspath.hpp"
#include "ui_dbuspath.h"
#include "qdbusviewer.h"
DbusPath::DbusPath(QDBusConnection bus,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DbusPath),m_bus(bus)
{
    ui->setupUi(this);

    ui->serviceFilterLine->setPlaceholderText(tr("Search..."));

    // Create model for services list
    servicesModel = new QStringListModel(this);
    // Wrap service list model in proxy for easy filtering and interactive sorting
    servicesProxyModel = new ServicesProxyModel(this);
    servicesProxyModel->setSourceModel(servicesModel);
    servicesProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);

    ui->servicesView->setModel(servicesModel);
    // Make services grid view behave like a list view with headers
    ui->servicesView->verticalHeader()->hide();
    ui->servicesView->horizontalHeader()->setStretchLastSection(true);
    ui->servicesView->setShowGrid(false);
    // Sort service list by default
    ui->servicesView->setSortingEnabled(true);
    ui->servicesView->sortByColumn(0, Qt::AscendingOrder);
    connect(ui->serviceFilterLine, SIGNAL(textChanged(QString)), servicesProxyModel, SLOT(setFilterFixedString(QString)));
    servicesModel->removeRows(0, servicesModel->rowCount());
    if (m_bus.isConnected()) {
        const QStringList serviceNames = m_bus.interface()->registeredServiceNames();
        QStringList strlist;
        QRegExp re(":[0-9]*\\.[0-9]+");
        foreach (QString var, serviceNames) {
            if(re.exactMatch (var))
            {
            }
            else if(var =="org.freedesktop.DBus")
            {
            }
            else
            {
                strlist<<var;
            }
        }

        servicesModel->setStringList(strlist);
    }
    connect(ui->servicesView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
            this, SLOT(serviceChanged(QModelIndex)));
}

DbusPath::~DbusPath()
{
    delete ui;
}
void DbusPath::pathChanged(const QModelIndex &index)
{
    currentPath = index.data(Qt::UserRole+1).toString();
    if(index.data(Qt::UserRole+2).toBool ())
    {
        currentInterface = "";
    }else
    {
        currentInterface = index.data(Qt::DisplayRole).toString();
    }
}

void DbusPath::serviceChanged(const QModelIndex &index)
{
//    disconnect(ui->tree->selectionModel (),0,0, 0);
    delete ui->tree->model();
    currentService.clear();
    if (!index.isValid())
        return;
    currentService = index.data().toString();
    currentPath = "/";
    currentInterface = "";

    ui->tree->setModel(new QDBusViewModel(currentService, m_bus,currentPath,currentInterface,false));
    connect(ui->tree->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
            this, SLOT(pathChanged(QModelIndex)));
}

void DbusPath::on_buttonBox_accepted()
{
    this->accept ();
}

void DbusPath::on_buttonBox_rejected()
{
    this->reject ();
}

QString DbusPath::getCurrentInterface() const
{
    return currentInterface;
}

QString DbusPath::getCurrentPath() const
{
    if(currentPath.isEmpty ())
        return "/";
    return currentPath;
}

QString DbusPath::getCurrentService() const
{
    return currentService;
}
